<?php
/**
 * 利用simpleXML 处理 xml 数据类
 * @author LSH 2013-02-17 
 */
class SimpleXML{
	
	/**
	 * 将simpleXML 对象转换为PHP 数组..
	 * @author  LSH 2013-02-17 
	 * 
	 * @param object $simplexml_obj   传入的simpleXML 数据对象
	 * @param INT $strip_white             是否清除左右空格
	 * @return array                                转换好的数组..
	 *
	 */
	public function xml2array($simplexml_obj, $strip_white=1)
	{
		$arr_ret			     = array();
		$array_all_key     = array();
		$array_need_key = array();
		
	
		if( $simplexml_obj )
		{
			if( count($simplexml_obj)==0 )
				return $strip_white?trim((string)$simplexml_obj):(string)$simplexml_obj;
		
			$attr = array();
			foreach ($simplexml_obj as $k=>$val)
			{
				if (isset($array_all_key[$k]))
				{
					if (!isset($array_need_key[$k]))
					{
						$array_need_key[$k] = 1;
					}
				}
				else
				{
					$array_all_key[$k] =1;
				}
			}
		
			foreach ($simplexml_obj as $k=>$val)
			{
		
				if( !empty($array_need_key) && isset($array_need_key[$k]) )
				{
					$arr_ret[$k][] = $this->xml2array($val, $strip_white);
				}
				else
				{
					$arr_ret[$k] = $this->xml2array($val, $strip_white);
				}
			}
			return $arr_ret;
		}
		
		return FALSE;
	}

	/**
	 * 将PHP 数组转换为 xml 数据..  
	 * 
	 * @param array $data             待转换的PHP 数组
	 * @param string $encoding    编码
	 * @param string $root             根节点..
	 * @param string $item             需要将数字编号给为字符的item..
	 * @param string $isAll             是否输出完整的Xml 
	 * @return string                       转换好的xml数据
	 */
	public function xml_encode($data, $encoding='utf-8', $root="root",$item ="item",$isAll = TRUE) 
	{
		$xml = "";
		if ($isAll)
		{
			$xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
			$xml.= '<' . $root . '>';
			$xml.= $this->data_to_xml($data,$item);
			$xml.= '</' . $root . '>';
		}
		else 
		{
			$xml.= $this->data_to_xml($data,$item);
		}
		
		
		
		return $xml;
	}
	
	
	private  function data_to_xml($data,$item)
	 {
		if (is_object($data)) 
		{
			$data = get_object_vars($data);
		}
		$xml = '';
		foreach ($data as $key => $val)
		 {
			is_numeric($key) && $key = $item;
			$xml.="<$key>";
			$xml.= ( is_array($val) || is_object($val)) ? $this->data_to_xml($val,$item) : $val;
			list($key, ) = explode(' ', $key);
			$xml.="</$key>";
		}
		
		return $xml;
	}
	
}


// 测试用例..
$string = <<<XML
<?xml version="1.0"?>
<GetCinemaResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResultCode>0</ResultCode>
  <Cinemas>
    <Cinema>
	<PlaceNo>1111</PlaceNo>
	<PlaceName>444</PlaceName>
     <CityNo>XXXX</CityNo>
     <CreateDate>XXXX</CreateDate>
    </Cinema>
    <Cinema>
	<PlaceNo>2222</PlaceNo>
	<PlaceName>XXXX</PlaceName>
     <CityNo>XXXX</CityNo>
     <CreateDate>XXXX</CreateDate>
    </Cinema>
  </Cinemas>
</GetCinemaResult>
XML;

//$xml = simplexml_load_string($string);
// print_r($xml);
// $login = $xml->login;//这里返回的依然是个SimpleXMLElement对象
// print_r($login);
// $login = (string) $xml->login;//在做数据比较时，注意要先强制转换
// print_r($login);

//$simpleXml = new SimpleXML();

//$ret = $simpleXml->xml2array($xml);
//print_r($ret);

//$toXml = $simpleXml($ret);

//echo $toXml;
